/* 
 * 
 * Created on 27 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package actions;

import java.net.InetAddress;
import java.net.URI;

import actions.threads.MonitorThread;
import actions.threads.ReceiveThread;

import data.WebSocket;

import objects.MessageCodes;
import objects.data.ClusterLoginData;
import objects.data.Message;
import utils.Utils;

/**
 *
 * @author simonk
 */
public class Login {
	public static void doLogin() throws Exception {
		Utils.out("+ connecting to server");
		Utils.ws = new WebSocket(new URI("ws://localhost:8085/"));
		Utils.ws.connect();
		Utils.out("+ connected");
		ClusterLoginData cluLoginData = new ClusterLoginData(InetAddress.getLocalHost().getHostName(),Utils.username,Utils.passwd);
		Utils.ws.send(cluLoginData);
		Utils.out("+ sent login data");
		String res = Utils.ws.recv().split("\\|")[1];
		Message resp = Message.fromJson(res);
		if (resp.type == MessageCodes.OK) {
			Utils.out("+ login success");
			new ReceiveThread().start();
			new MonitorThread().start();
			Utils.out("+ waiting for jobs");
		} else {
			Utils.out("- login failed: "+resp.msg);
		}
	}
}
